package com.buba.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.alipay.api.domain.Data;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import common.R;
import entity.device.PlaceForStorage;
import com.buba.mapper.PlaceForStorageMapper;
import com.buba.service.PlaceForStorageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import java.util.List;

/**
 * <p>
 * 存放位置表 服务实现类
 * </p>
 *
 * @author beisheng
 * @since 2023-05-18
 */
@Service
public class PlaceForStorageServiceImpl extends ServiceImpl<PlaceForStorageMapper, PlaceForStorage> implements PlaceForStorageService {
    @Autowired
    private PlaceForStorageMapper placeForStorageMapper;


    /**
     * 新增
     *
     * @param placeForStorage
     * @return
     */
    @Override
    public boolean insetStorage(PlaceForStorage placeForStorage) {
        placeForStorage.setId(null);
        placeForStorage.setCreateTime(new Date());
        placeForStorage.setDeleteFlag(0);
        int insert = placeForStorageMapper.insert(placeForStorage);
        return insert > 0;
    }

    /**
     * 存放位置列表查询
     *
     * @return
     */
    @Override
    public List<PlaceForStorage> queryAll(String locationName) {

        if ("".equals(locationName)) {
            QueryWrapper<PlaceForStorage> wrapper = new QueryWrapper<>();
            wrapper.eq("delete_flag", 0);
            return placeForStorageMapper.selectList(wrapper);
        } else {
            QueryWrapper<PlaceForStorage> wrapper = new QueryWrapper<>();
            wrapper.like("location_name", locationName);
            wrapper.eq("delete_flag", 0);
            return placeForStorageMapper.selectList(wrapper);

        }

    }


    /* *
     * @Author: beisheng
     * @Descriprion: 查询所有设备存放位置
     * @Date: 2023/5/22 14:27
     * @Return: common.R
     * @Param: []
     */

    @Override
    public List<PlaceForStorage> selPlace() {
        List<PlaceForStorage> placeForStorages = placeForStorageMapper.selectList(new QueryWrapper<PlaceForStorage>().eq("delete_flag", 0));
        return placeForStorages;
    }
    /**
     * 编辑列表
     *
     * @param placeForStorage
     * @return
     */
    @Override
    public boolean updateStorage(PlaceForStorage placeForStorage) {
        placeForStorage.setUpdateTime(new Date());
        UpdateWrapper<PlaceForStorage> UpdateWrapper = new UpdateWrapper<>();
        UpdateWrapper.eq("id", placeForStorage.getId());
        UpdateWrapper.eq("delete_flag", 0);
        int update = placeForStorageMapper.update(placeForStorage, UpdateWrapper);
        return update > 0;
    }

    /**
     * 逻辑删除
     *
     * @param idArray
     * @return
     */
    @Transactional
    @Override
    public boolean deleteAll(List<Integer> idArray) {
        int i = placeForStorageMapper.deleteBatchIds(idArray);
        return i > 0;
    }

    /**
     * 启动
     *
     * @param idArray
     * @return
     */
    @Transactional
    @Override
    public boolean startUsing(Integer[] idArray) {
        // String result = String.join(",", Arrays.stream(idArray).map(String::valueOf).toArray(String[]::new));
        boolean b = false;
        for (Integer s : idArray) {
            b = placeForStorageMapper.start(s);
        }
        return b;
    }

    /**
     * 批量停用
     *
     * @param idArray
     * @return
     */
    @Transactional
    @Override
    public boolean outService(Integer[] idArray) {
        boolean b = false;
        for (Integer s : idArray) {
            b = placeForStorageMapper.outService(s);
        }
        return b;
    }

    /**
     * @return java.util.List<entity.device.PlaceForStorage>
     * @Author atlong
     * @Description 查询已开启的设备位置信息
     * @Date 2023/6/1 20:05
     * @Param []
     **/
    @Override
    public List<PlaceForStorage> selAllStorage() {
        QueryWrapper<PlaceForStorage> wrapper = new QueryWrapper<>();
        wrapper.eq("delete_flag",0);
        wrapper.eq("state",1);
        List<PlaceForStorage> placeForStorages = placeForStorageMapper.selectList(wrapper);
        return placeForStorages;
    }
}
